import request from "@/utils/request";
const defaultApi = (apiName) => {
    return {
        list: (params) => request.get(`/${apiName}/list`, { params }),
        simpleList: (params) => request.get(`/${apiName}/simpleList`, { params }),
        page: (params) => request.get(`/${apiName}`, { params }),
        read: (id) => request.get(`/${apiName}/` + id),
        create: (id, data) => request.post(`/${apiName}`, data),
        update: (id, data) => request.put(`/${apiName}/` + id, { ...data, create_time: undefined, id: undefined }),
        delete: (id) => request.delete(`/${apiName}/` + id)
    }
}
export const http = {
    login: (data) => request.post('/user/login', data),
    menu: defaultApi('menu'),
    role: defaultApi('role'),
    user: {
        ...defaultApi('user'),
        getUserInfo: (userId) => request.get(`/user/getUserInfo`, { params: { userId }}),
        getUserRoles: (userId) => request.get(`/user/getUserRoles`, { params: { userId }}),
        updateUserRoles: (data) => request.post(`/user/updateUserRoles`, data),
    },
    member: defaultApi('member'),
    // 图书馆管理
    libraries: {
        ...defaultApi('libraries'),
        getLibraryStats: (id) => request.get(`/libraries/stats/${id}`),
    },
    libraryFloors: defaultApi('libraryFloors'),
    libraryAreas: defaultApi('libraryAreas'),
    seats: {
        ...defaultApi('seats'),
        batchCreate: (data) => request.post('/seats/batchCreate', data),
        updateStatus: (id, status) => request.put(`/seats/status/${id}`, { status }),
    },
    
    // 预约管理
    reservations: {
        ...defaultApi('reservations'),
        approve: (id) => request.put(`/reservations/approve/${id}`),
        reject: (id, reason) => request.put(`/reservations/reject/${id}`, { reason }),
        checkIn: (id) => request.put(`/reservations/checkIn/${id}`),
        checkOut: (id) => request.put(`/reservations/checkOut/${id}`),
        cancel: (id) => request.put(`/reservations/cancel/${id}`),
        getStats: (params) => request.get('/reservations/statistics', { params }),
    },
    
    // 用户管理
    miniProgramUsers: {
        ...defaultApi('miniProgramUsers'),
        block: (id) => request.put(`/miniProgramUsers/block/${id}`),
        unblock: (id) => request.put(`/miniProgramUsers/unblock/${id}`),
    },
    students: defaultApi('students'),
    schools: defaultApi('schools'),
    
    // 内容管理
    news: {
        ...defaultApi('news'),
        publish: (id) => request.put(`/news/publish/${id}`),
        unpublish: (id) => request.put(`/news/unpublish/${id}`),
    },
    carousels: {
        ...defaultApi('carousels'),
        updateSort: (id, sort) => request.put(`/carousels/sort/${id}`, { sort }),
    },
}
// uploadFile
export const uploadFile = (file) => {
    const formData = new FormData()
    formData.append('file', file)
    return request({
        url: '/upload/file',
        method: 'post',
        data: formData
    })
}

// 上传图片（返回图片URL）
export const uploadImage = (file) => {
    const formData = new FormData()
    formData.append('file', file)
    return request({
        url: '/upload/image',
        method: 'post',
        data: formData
    })
}
